package com.zyb.warehouse.service.impl;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.CurrentUser;
import com.zyb.warehouse.entity.InStore;
import com.zyb.warehouse.entity.Purchase;
import com.zyb.warehouse.mapper.InStoreMapper;
import com.zyb.warehouse.mapper.PurchaseMapper;
import com.zyb.warehouse.service.PurchaseService;
import com.zyb.warehouse.service.StoreService;
import com.zyb.warehouse.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:采购表业务层接口的实现类
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 添加采购单
     *
     * @param purchase
     * @return
     */
    public Result addPurchase(Purchase purchase) {
        //进行添加
        Integer i = purchaseMapper.insertPurchase(purchase);
        if (i > 0) {
            return Result.ok("添加采购单成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "添加采购单失败");
    }

    /**
     * 分页查询采购单
     *
     * @param page
     * @param purchase
     * @return
     */
    public Page pagePurchase(Page page, Purchase purchase) {
        //获取采购表总记录数
        Integer count = purchaseMapper.selectPurchaseTotal(purchase);
        //开始分页
        List<Purchase> purchaseList = purchaseMapper.pagePurchase(page, purchase);
        //封装数据
        page.setTotalNum(count);
        page.setResultList(purchaseList);
        return page;
    }

    /**
     * 删除采购单
     *
     * @param buyId
     * @return
     */
    public Integer removePurchase(Integer buyId) {
        return purchaseMapper.deletePurchase(buyId);
    }

    /**
     * 修改采购单
     *
     * @param purchase
     * @return
     */
    public Result modifyPurchase(Purchase purchase) {
        //直接修改
        Integer i = purchaseMapper.updatePurchase(purchase);
        if (i > 0) {
            return Result.ok("修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
    }

    /**
     * 生成入库单
     *
     * @param purchase
     * @return
     */
    @Transactional
    public Result addInStore(Purchase purchase, String token) {
        //从前端获取登录人信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //创建入库单对象
        InStore inStore = new InStore();
        //属性拷贝
        inStore.setStoreId(purchase.getStoreId());
        inStore.setProductId(purchase.getProductId());
        inStore.setInNum(purchase.getFactBuyNum());
        inStore.setCreateBy(currentUser.getUserId());

        //生成入库单
        Integer i = inStoreMapper.insertInStore(inStore);
        if (i > 0) {
            Integer integer = purchaseMapper.updatePurchaseState(purchase.getBuyId());
            if (integer > 0) {
                return Result.ok("生成入库单成功");
            }
            return Result.err(Result.CODE_ERR_BUSINESS, "生成入库单失败");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "生成入库单失败");
    }
}
